<template>
  <div id="app">
    <Inventory @update-items="updateItems" ref="inventory" />
    <Character
      @update-slots="updateSlots"
      @add-item="addItemToSlot"
      ref="character"
    />
    <Character
      @update-slots="updateSlots"
      @add-item="addItemToSlot"
      @remove-item="removeItemFromSlot"
      ref="character"
    />
  </div>
</template>

<script>
import Inventory from './components/Inventory.vue';
import Character from './components/Character.vue';

export default {
  name: 'App',
  components: {
    Inventory,
    Character,
  },
  data() {
    return {
      allItems: [],
      characterSlots: [],
    };
  },
  methods: {
    updateItems(items) {
      this.allItems = items;
    },
    updateSlots(slots) {
      this.characterSlots = slots;
    },
    addItemToSlot(item) {
      const inventory = this.$refs.inventory;
      const itemIndex = this.allItems.findIndex((i) => i.id === item.id);
      if (itemIndex !== -1) {
        this.allItems.splice(itemIndex, 1);
        this.$refs.character.addItem(item);
      }
    },
    removeItemFromSlot(item) {
      const slotIndex = this.characterSlots.findIndex(
        (slot) => slot.item && slot.item.id === item.id
      );
      if (slotIndex !== -1) {
        this.characterSlots[slotIndex].item = null;
        this.allItems.push(item);
      }
    },
  },
};
</script>

<style>
#app {
  display: flex;
  justify-content: space-between;
  padding: 20px;
}
</style>
